*************************************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*			                       Replication file for:     		           	        	    *
*       "The Effects of Government System Fractionalization on Satisfaction with Democracy"     *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*																						 		*			
* Author: 			Pablo Christmann and Mariano Torcal                                   		*
* Contact: 			pablo.christmann@upf.edu                                              		*
* Date: 			16th of May 2017                                                            *
* Version:			Stata 13.1                                                              	*                                                                          
* Dataset:          TSCS.dta                                                					*		
* Logfile: 			TSCS analysis.txt															*
*																								*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*************************************************************************************************

set more off

*** Install required packages
ssc install outreg2
ssc install norm
ssc install labutil 
*net install http://www.ats.ucla.edu/stat/stata/ado/analysis/collin.pkg

*** Set directory to the location where replication files have been saved, for example: 
cd "C:\PSRM Replication Files"

*** Opening log
capture log close _all
log using TSCS_analysis.log, name(TSCS_Log) replace 

*** Load TSCS panel dataset
use "TSCS.dta", clear

********************************************************************
*** Generate 'between' and 'within' terms at the electoral level ***
********************************************************************
*** Notes: Depending on their level of measurment (country-year or election period) 
***        the "between" terms (country averages) and the longitudional within terms need to be estimated seperately. 
***        The estimation of interaction terms, squared terms and logged variables need to be done before with the original data!

*** Generate averages of SWD by election periods (required for Figure 1)
egen swd_perc_mean = mean(swd_perc), by(elec_id)

*** Keep only one observation per election as not to give any election period more weight in the calculations simply because it is covered more often.
bysort elec_id: gen counter_n= _n
keep if counter_n==1
drop counter_n 

*** Generation of squared terms
gen enep1_sq=enep1*enep1
gen gallager_index_sq=gallager_index*gallager_index

*** Generation of logged term
gen tier1_avemag_log=log(tier1_avemag)

*** Generation of country mean terms
egen enpp1_mean = mean(enpp1), by(country)
egen enep1_mean = mean(enep1), by(country)
egen gallager_index_mean = mean(gallager_index), by(country)
egen tier1_avemag_mean = mean(tier1_avemag), by(country)
egen tier1_avemag_log_mean = mean(tier1_avemag_log), by(country)
egen enep1_sq_mean = mean(enep1_sq), by(country)
egen gallager_index_sq_mean = mean(gallager_index_sq), by(country)

*** Generation of within terms
gen enpp1_w = enpp1 - enpp1_mean
gen enep1_w = enep1 - enep1_mean
gen gallager_index_w = gallager_index - gallager_index_mean

******************************
*** Generation of Figure 1 ***
******************************
*** Note: Figure 1 has been formatted with the Graph Editor afterwards. 
***       The corresponding .gph files have been uploaded in the folder "Figures and Tables".

*** Estimate percintiles for election periods with very high, high, low and very low SWD.
sum swd_perc_mean, detail
gen swd_perc_mean_cat=""
replace swd_perc_mean_cat="Very Low" if swd_perc_mean<= 36.0115
replace swd_perc_mean_cat="Low" if swd_perc_mean> 36.0115 & swd_perc_mean <= 49.58917
replace swd_perc_mean_cat="High" if swd_perc_mean> 49.58917 & swd_perc_mean <= 66.23479
replace swd_perc_mean_cat="Very High" if swd_perc_mean> 66.23479

*** Figure 1:
graph twoway (scatter enep1 gallager_index if swd_perc_mean_cat=="Very Low", msymbol(triangle) msize(medsmall) mcolor(gs12) legend(label(1 "Very low SWD"))) (scatter enep1 gallager_index if swd_perc_mean_cat=="Low", msymbol(triangle) msize(medsmall) mcolor(gs8) legend(label(2 "Low SWD"))) (scatter enep1 gallager_index if swd_perc_mean_cat=="High", msymbol(circle) msize(medium) mcolor(gs6) legend(label(3 "High SWD"))) (scatter enep1 gallager_index if swd_perc_mean_cat=="Very High" , msymbol(circle) msize(medium) mcolor(black) legend(label(4 "Very High SWD"))) 
graph save "Figure 1.gph", replace

*** Keep only the "between" and "within" terms of the electoral variables that vary by election periods
keep elec_id  enpp1_mean enep1_mean gallager_index_mean tier1_avemag_mean tier1_avemag_log_mean enep1_sq_mean gallager_index_sq_mean enpp1_w enep1_w gallager_index_w   

*** Save this dataset for merge with the original dataset 
save "TSCS_help.dta", replace

*******************************************************************
*** Generate 'between' and 'within' terns at the country level  ***
*******************************************************************
*** Load TSCS panel dataset
use "TSCS.dta", clear
merge n:1 elec_id using "TSCS_help.dta" // add country means and within terms at the electoral level
drop _merge

*** Generation of squared term
gen govfrac_sq=govfrac*govfrac

*** Generation of interaction term
gen govfrac_gallager=govfrac*gallager_index
gen enep1_gallager=enep1*gallager_index

*** Generation of group mean terms
egen swd_perc_mean = mean(swd_perc), by(country)
egen growth_rate_mean = mean(growth_rate), by(country)
egen GDPpc_mean = mean(wdi_gdppccur), by(country)
egen gini_mean = mean(gini), by(country)
egen icrg_qog_mean = mean(icrg_qog), by(country)
egen govfrac_mean = mean(govfrac), by(country) 
egen govfrac_sq_mean = mean(govfrac_sq), by(country) 
egen years_election_mean = mean(years_election), by(country)
egen govfrac_gallager_mean = mean(govfrac_gallager), by(country) 
egen enep1_gallager_mean = mean(enep1_gallager), by(country)

*** Generation of within terms 
gen swd_perc_w = swd_perc - swd_perc_mean
gen growth_rate_w = growth_rate - growth_rate_mean
gen gini_w = gini - gini_mean
gen icrg_qog_w = icrg_qog - icrg_qog_mean
gen govfrac_w = govfrac - govfrac_mean
gen years_election_w = years_election - years_election_mean

**********************************************
*** Standardization of Continous Variables ***
**********************************************
*** Note: All standardized variables have the prefix zee_ 
norm enpp1_mean, method(zee)
norm enep1_mean, method(zee)
norm gallager_index_mean, method(zee)
norm tier1_avemag_mean, method(zee)
norm tier1_avemag_log_mean, method(zee)
norm years_election_mean, method(zee)
norm enep1_sq_mean, method(zee)
norm gallager_index_sq_mean, method(zee)
norm govfrac_sq_mean, method(zee)
norm growth_rate_mean, method(zee)
norm GDPpc_mean, method(zee)
norm icrg_qog_mean, method(zee)
norm govfrac_mean, method(zee)
norm gini_mean, method(zee)
norm govfrac_gallager_mean , method(zee)
norm enep1_gallager_mean , method(zee)
norm enpp1_w, method(zee)
norm enep1_w, method(zee)
norm gallager_index_w, method(zee)
norm years_election_w, method(zee)
norm growth_rate_w, method(zee)
norm gini_w, method(zee)
norm icrg_qog_w, method(zee)
norm govfrac_w, method(zee)
norm al_ethnic, method(zee)
norm time_trend , method(zee)

*********************************
*** Generation of Line Graphs ***
*********************************
*** Note: The line graphs have been formatted with the Graph Editor afterwards. 
***       The corresponding .gph files have been uploaded in the folder "Figures and Tables".

encode country, gen(country_id)
labmask country_id, values(country)
tsset country_id year, yearly

*** Generate Figure 2 
gen swd_perc_div10 = swd_perc/10 // rescale SWD to a 0-10 pt scale
xtline swd_perc_div10 enep1 , ylabel(0(5)10)
graph save "Figure 2.gph", replace

*** Generate Figure A 
xtline gallager_index , ylabel(0(10)20) 
graph save "Figure A.gph", replace

*** Generate Figure B 
xtline govfrac , ylabel(0(0.4)0.8) 
graph save "Figure B.gph", replace

****************************************************
*** Table 1: Multilevel Analysis of TSCS Dataset ***
****************************************************
*** Notes: The post-estimation commands estat ic and estat icc are used to estimate the AIC and the Intraclass Correlation Coefficient (ICC) after each model.
***        The table has been created based on the information conveyed by outreg2 and the Stata output (Variance components, Log likelihood, AIC and ICCs)

*** Model 1 (Null)
xtmixed swd_perc || country: || elec_id: , mle variance 
outreg2 using "Table 1.out", replace ctitle(Model 1) dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*) sideway
estat ic // Get AIC values
estat icc // Get Intraclass correlation coefficient (ICC) for the country-level
display 1-0.8377917 // Estimate ICC for the country-year level
display 0.8377917-0.6941167 // Estimate ICC for the election-level

*** Notes on the ICCs: estat icc provides two values (country and elec_id|country). The ICC values for "country" provides information about the share of variation that can 
*   be attributed to the county-level (=ICC Country in Table 1, Model 1). 
*   "elec_id|country" provides the information about how much variation can be attributed jointly to to the country-level and the election-level.
*   To estimate the ICC for the country-year level, simply substract the variation that can be attributed to the country-level and election-level (elec_id|country) from the total varition 1.
*   ICC Country year = 1-0.8377917 = 0.1622083.
*   To get the ICC for the election-level, substract the variation attributable to the country-level (country) from the shared variation of the country and election-level (elec_id|country).
*   ICC Election = 0.8377917-0.6941167 = 0.143675 

*** Model 2 (Government Fractionalization and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic ///
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_govfrac_mean ///
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_govfrac_w zee_time_trend || country: || elec_id: , mle variance 	
outreg2 using "Table 1.out",   append ctitle(Model 2)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic 
estat icc 

*** Model 3 (ENEP and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic ///        
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_enep1_mean ///
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_enep1_w zee_time_trend || country: || elec_id: , mle variance 
outreg2 using "Table 1.out",   append ctitle(Model 3)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic 
estat icc 

*******************************************************************************
*** Table D: Electoral System and Average District Magnitude (TSCS Dataset) ***
*******************************************************************************
*** Notes: estat ic and estat icc are used to estimate the AIC and the Intraclass Correlation Coefficient after each model
***        The table has been created based on the information conveyed by outreg2 and the Stata output (Variance components, Log likelihood, AIC and ICCs)

*** Model 8 (Electoral System)
xtmixed swd_perc i.regime federal zee_al_ethnic i.electoral_system ///  
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean ///   
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_time_trend || country: || elec_id: , mle variance 	
outreg2 using "Table D.out",   replace ctitle(Model 8)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic 
estat icc

*** Model 9 (Average District Magnitude)
xtmixed swd_perc i.regime federal zee_al_ethnic zee_tier1_avemag_mean ///  
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_time_trend || country: || elec_id: , mle variance 
outreg2 using "Table D.out",   append ctitle(Model 9)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic
estat icc

/* Not shown in Table D 
*** Model 9_A (Average District Magnitude, Logged)
xtmixed swd_perc i.regime federal zee_al_ethnic zee_tier1_avemag_log_mean ///
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean ///   
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_time_trend || country: || elec_id: , mle variance 
outreg2 using "Table D.out",   append ctitle(Model 9_A)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic
estat icc
*/

***********************************************************************************************************
*** Table F: Party/ Government System Fractionalization and Electoral Disproportionality (TSCS Dataset) ***
***********************************************************************************************************
*** Notes: estat ic and estat icc are used to estimate the AIC and the Intraclass Correlation Coefficient after each model
***        The table has been created based on the information conveyed by outreg2 and the Stata output (Variance components, Log likelihood, AIC and ICCs)

*** Model 12 (Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean ///
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_time_trend || country: || elec_id: , mle variance
outreg2 using "Table F.out",   replace ctitle(Model 12)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic
estat icc

*** Model 13 (Gallagher Index squared)
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_gallager_index_sq_mean ///  
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_time_trend || country: || elec_id: , mle variance 
outreg2 using "Table F.out",   append ctitle(Model 13)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic
estat icc

*** Model 14 (ENEP)
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean  zee_enep1_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_enep1_w zee_time_trend || country: || elec_id: , mle variance 
outreg2 using "Table F.out",   append ctitle(Model 14)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic
estat icc

*** Model 15 (ENEP squared)
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean  zee_enep1_mean zee_enep1_sq_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_enep1_w zee_time_trend || country: || elec_id: , mle variance 
outreg2 using "Table F.out",   append ctitle(Model 15)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic
estat icc

*** Model 16 (ENEP * Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_enep1_mean zee_enep1_gallager_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_enep1_w zee_time_trend || country: || elec_id: , mle variance 
outreg2 using "Table F.out",   append ctitle(Model 17)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic
estat icc

*** Model 17 (Government Fractionalization)
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_govfrac_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_govfrac_w zee_time_trend || country: || elec_id: , mle variance 
outreg2 using "Table F.out",   append ctitle(Model 16)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic
estat icc

*** Model 18 (Government Fractionalization * Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_govfrac_mean zee_govfrac_gallager_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_govfrac_w zee_time_trend || country: || elec_id: , mle variance 
outreg2 using "Table F.out",   append ctitle(Model 18)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic
estat icc

************************************************************************************************
*** Table H: Government System Fractionalization (TSCS Dataset, Parliamentary Systems Only)  ***
************************************************************************************************
*** Notes: estat ic and estat icc are used to estimate the AIC and the Intraclass Correlation Coefficient after each model
***        The table has been created based on the information conveyed by outreg2 and the Stata output (Variance components, Log likelihood, AIC and ICCs)

*** Keep only parliamentary systems, create temporary file of the used dataset
save "TSCS_temp.dta", replace

drop if regime==2 // drop if Presidential
drop if regime==1 // drop if Semi-Presidential

*** Model 27 (Government Fractionalization and Gallagher Index)
xtmixed swd_perc federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_govfrac_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_govfrac_w zee_time_trend || country: || elec_id: , mle variance
outreg2 using "Table H.out",   replace ctitle(Model 27)  dec(2) onecol alpha(0.001,0.01,0.05) symbol(***,**,*)  sideway
estat ic
estat icc

use "TSCS_temp.dta", clear // restore data


************************************************
************************************************
*** End of Analysis Reported in the Article  ***
************************************************
************************************************


***********************************************************************************
*** Additional Robustness Checks mentioned in the Supplementary Online Appendix ***
***********************************************************************************
*** Notes: 	We performed six additional robustness checks for the results presented in Table 1 which we describe only in the Appendix. 
***        	1.) Comparison of robust standard errors, 2.) random slopes for ENEP and Years since Last Election,
***			3.) treating �country-years� as nested within �election cycles,� which are cross-classified within �years� and �countries.
***         4.) inspections of model residuals, 5.) gauge the degree of collinearity,
***			6.) analysis the errors u0 and u1 (country and election), control for influential cases at the country and election level


******************************************
*** Table 1: 1.)Robust Standard Errors ***
******************************************
*** Notes: 	Results are robust, no coefficient loses or gains much in significance.

*** Model 2 (Government Fractionalization and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic ///
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_govfrac_mean ///
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_govfrac_w zee_time_trend || country: || elec_id: , mle variance robust 	

*** Model 3 (ENEP and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic ///
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_enep1_mean ///
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_enep1_w zee_time_trend || country: || elec_id: , mle variance robust 

*********************************
*** Table 1: 2.)Random Slopes ***
*********************************
*** Notes: 	Results for Enep and years since election are robust against the inclusion of random slopes.

*** Model 2 (Government Fractionalization and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic ///
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_govfrac_mean ///
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_govfrac_w zee_time_trend || country: zee_years_election_w || elec_id: zee_govfrac_w , mle variance  

*** Model 3 (ENEP and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic ///
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_enep1_mean ///
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_enep1_w zee_time_trend || country: zee_years_election_w || elec_id: zee_enep1_w , mle variance 	

***********************************************************
*** Table 1: 3.)Year as another level (see notes above) ***
***********************************************************

*** Model 2 (Government Fractionalization and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic ///
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_govfrac_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_govfrac_w zee_time_trend || _all: R.country || _all: R.year || elec_id : , mle  variance

*** Model 3 (ENEP and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_enep1_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_enep1_w zee_time_trend || _all: R.country || _all: R.year || elec_id : , mle  variance

***********************************
*** Table 1: 4.)Model Residuals ***
***********************************
*** Notes: 	The results of the models are not sensitive to the exclusion of large residuals

*** Create temporary file of the used dataset
save "TSCS_temp.dta", replace

**************************************************************
*** Model 2 (Government Fractionalization and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic  ///
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_govfrac_mean ///
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_govfrac_w zee_time_trend || country: || elec_id: , mle variance 

* Inspect residuals: Very few problematic cases
predict resid, residuals
predict resid_std, rstandard
swilk resid_std
sktest resid_std
qnorm resid_std

* Drop very large residuals to gauge their effect on the estimates
drop if resid_std<=-3
drop if resid_std>=2.2

*** Reestimate Model 2: Results are robust
xtmixed swd_perc i.regime federal zee_al_ethnic  ///
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_govfrac_mean ///
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_govfrac_w zee_time_trend || country: || elec_id: , mle variance 

use "TSCS_temp.dta", clear // restore data

**************************************
*** Model 3 (ENEP and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_enep1_mean  /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_enep1_w zee_time_trend || country: || elec_id: , mle variance 

* Inspect residuals: Very few problematic cases
predict resid, residuals
predict resid_std, rstandard
swilk resid_std
sktest resid_std
qnorm resid_std

* Drop very large residuals to gauge their effect on the estimates
drop if resid_std<=-3
drop if resid_std>=2.3

* Reestimate Model 3: Results are robust
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_enep1_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_enep1_w zee_time_trend || country: || elec_id: , mle variance

use "TSCS_temp.dta", clear // restore data

********************************
*** Table 1: 5.)Collinearity ***
********************************
*** Model 2 (Government Fractionalization and Gallagher Index)
collin regime federal zee_al_ethnic zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_govfrac_mean zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_govfrac_w 
*** Model 3 (ENEP and Gallagher Index)
collin regime federal zee_al_ethnic zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_enep1_mean zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_enep1_w 

***************************************************************
*** Table 1: 6.) Analyzing u0 and u1 (Country and Election) ***
***************************************************************
*** Create temporary file of the used dataset
save "TSCS_temp.dta", replace

**************************************************************
*** Model 2 (Government Fractionalization and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic  /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_govfrac_mean  /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_govfrac_w zee_time_trend || country: || elec_id: , mle variance  

predict u_country u_election, reffects 

* Search for influential cases at the country level
sort u_country
gen rank0= _n
bysort country: egen secuencia0=seq()
scatter u_country rank0 if secuencia0==1, mlabel(country) yline(0) // Uruguay, Italy
twoway (scatter u_country zee_gallager_index_mean  if secuencia0==1, mlabel(country) sort) (lfit u_country zee_gallager_index_mean if secuencia0==1, sort) // Uruguay, Italy, Denmark
twoway (scatter u_country zee_govfrac_mean  if secuencia0==1, mlabel(country) sort) (lfit u_country zee_govfrac_mean if secuencia0==1, sort) // Uruguay, Italy, Denmark

* gen country dummies
gen Uruguay = 0
replace Uruguay = 1 if country=="Uruguay"
gen Denmark = 0
replace Denmark = 1 if country=="Denmark"
gen Italy = 0
replace Italy = 1 if country=="Italy"

* Search for influential cases at the election level (the within term of government fractionalization is a country-year level variable and therefore not covered here)
sort u_election
gen rank1= _n
bysort elec_id: egen secuencia1=seq()
scatter u_election rank1 if secuencia1==1, mlabel() yline(0)
twoway (scatter u_election zee_gallager_index_w  if secuencia1==1, mlabel(elec_id) sort) (lfit u_election zee_gallager_index_w if secuencia1==1, sort)

* gen election period dummies
tab elec_id if u_election<-10
gen BEL_1995 =0
replace BEL_1995 =1 if elec_id=="L-BEL-1995-5-21"
gen BUL_1994 =0
replace BUL_1994 =1 if elec_id=="L-BUL-1994-12-18"
gen IRE_2011 =0
replace IRE_2011 =1 if elec_id=="L-IRE-2011-2-25"
gen POR_2011 =0
replace POR_2011 =1 if elec_id=="L-POR-2011-6-5"
gen ITA_1992 =0
replace ITA_1992 =1 if elec_id=="L-ITA-1992-4-5"

tab elec_id if u_election>10
gen PAN_2009 =0
replace PAN_2009 =1 if elec_id=="L-PAN-2009-5-3"
gen POR_1991 =0
replace POR_1991 =1 if elec_id=="L-POR-1991-10-6"
gen POR_1987 =0
replace POR_1987 =1 if elec_id=="L-POR-1987-7-19"
gen TUR_2002 =0
replace TUR_2002 =1 if elec_id=="L-TUR-2002-11-3"

* Reestimate Model 2: Results are robust
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_govfrac_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_govfrac_w zee_time_trend ///
Uruguay Denmark Italy BEL_1995 BUL_1994 IRE_2011 POR_2011 ITA_1992 PAN_2009 POR_1991 POR_1987 TUR_2002 || country: || elec_id: , mle variance  

use "TSCS_temp.dta", clear // restore data

**************************************
*** Model 3 (ENEP and Gallagher Index)
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_enep1_mean /// 
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_enep1_w zee_time_trend || country: || elec_id: , mle variance

predict u_country u_election, reffects 

* Search for influential cases at the country level
sort u_country
gen rank0= _n
bysort country: egen secuencia0=seq()
scatter u_country rank0 if secuencia0==1, mlabel(country) yline(0) // Uruguay, Denmark
twoway (scatter u_country zee_enep1_mean  if secuencia0==1, mlabel(country) sort) (lfit u_country zee_enep1_mean if secuencia0==1, sort) // Uruguay, Denmark, Belgium, Brazil
twoway (scatter u_country zee_gallager_index_mean  if secuencia0==1, mlabel(country) sort) (lfit u_country zee_gallager_index_mean if secuencia0==1, sort) // Uruguay, Denmark, France

* gen country dummies
gen Uruguay = 0
replace Uruguay = 1 if country=="Uruguay"
gen Denmark = 0
replace Denmark = 1 if country=="Denmark"
gen Belgium = 0
replace Belgium = 1 if country=="Belgium"
gen Brazil = 0
replace Brazil = 1 if country=="Brazil"
gen France = 0
replace France = 1 if country=="France"

* Search for influential cases at the election level
sort u_election
gen rank1= _n
bysort elec_id: egen secuencia1=seq()
scatter u_election rank1 if secuencia1==1, mlabel() yline(0)
twoway (scatter u_election zee_enep1_w  if secuencia1==1, mlabel(elec_id) sort) (lfit u_election zee_enep1_w if secuencia1==1, sort)
twoway (scatter u_election zee_gallager_index_w  if secuencia1==1, mlabel(elec_id) sort) (lfit u_election zee_gallager_index_w if secuencia1==1, sort)

* gen election period dummies
tab elec_id if u_election<-10
gen BEL_1995 =0
replace BEL_1995 =1 if elec_id=="L-BEL-1995-5-21"
gen BUL_1994 =0
replace BUL_1994 =1 if elec_id=="L-BUL-1994-12-18"
gen IRE_2011 =0
replace IRE_2011 =1 if elec_id=="L-IRE-2011-2-25"
gen POR_2011 =0
replace POR_2011 =1 if elec_id=="L-POR-2011-6-5"
gen COL_2006 =0
replace COL_2006 =1 if elec_id=="L-COL-2006-3-12"
gen BRA_2010 =0
replace BRA_2010 =1 if elec_id=="L-BRA-2010-10-3"

tab elec_id if u_election>10
gen PAN_2009 =0
replace PAN_2009 =1 if elec_id=="L-PAN-2009-5-3"
gen POR_1991 =0
replace POR_1991 =1 if elec_id=="L-POR-1991-10-6"
gen POR_1987 =0
replace POR_1987 =1 if elec_id=="L-POR-1987-7-19"
gen TUR_2002 =0
replace TUR_2002 =1 if elec_id=="L-TUR-2002-11-3"

* Reestimate Model 3: Results are robust
xtmixed swd_perc i.regime federal zee_al_ethnic /// 
zee_GDPpc_mean zee_growth_rate_mean zee_gini_mean zee_icrg_qog_mean zee_gallager_index_mean zee_enep1_mean ///
zee_growth_rate_w zee_gini_w zee_icrg_qog_w zee_years_election_w zee_gallager_index_w zee_enep1_w zee_time_trend  /// 
Uruguay Denmark Belgium Brazil France BEL_1995 BUL_1994 IRE_2011 POR_2011 PAN_2009 POR_1991 POR_1987 TUR_2002 COL_2006 BRA_2010 || country: || elec_id: , mle variance 

use "TSCS_temp.dta", clear // restore data

***************************************************************************
*** Results for Table 1 are robust on every tested aspect, end analysis ***
***************************************************************************
log close TSCS_Log
